﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Web7.Utilities
{
    /// <summary>
    /// 
    /// </summary>
    public class HtmlUtils
    {
        /// <summary>
        /// 格式化Email
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public static string FormatEmail(string email)
        {
            return FormatEmail(email, true);
        }

        /// <summary>
        /// 格式化Email
        /// </summary>
        /// <param name="email"></param>
        /// <param name="replaceAtMark"></param>
        /// <returns></returns>
        public static string FormatEmail(string email, bool replaceAtMark)
        {
            string str = string.Empty;
            if (string.IsNullOrEmpty(email))
            {
                return str;
            }
            string str2 = replaceAtMark ? email.Replace('@', '#') : email;
            if (email.IndexOfAny(new char[] { '@', '#' }) > -1)
            {
                return string.Format("<a herf=\"mailto:{0}\">{1}</a>", email.Replace('#', '@'), str2);
            }
            return email;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stringToStrip"></param>
        /// <returns></returns>
        public static string StripAllTags(string stringToStrip)
        {
            return StripAllTags(stringToStrip, true);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stringToStrip"></param>
        /// <param name="enableMultiLine"></param>
        /// <returns></returns>
        public static string StripAllTags(string stringToStrip, bool enableMultiLine)
        {
            if (enableMultiLine)
            {
                stringToStrip = Regex.Replace(stringToStrip, @"</p(?:\s*)>(?:\s*)<p(?:\s*)>", "\n\n", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                stringToStrip = Regex.Replace(stringToStrip, @"<br(?:\s*)/>", "\n", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            }
            else
            {
                stringToStrip = Regex.Replace(stringToStrip, @"</p(?:\s*)>(?:\s*)<p(?:\s*)>", string.Empty, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                stringToStrip = Regex.Replace(stringToStrip, @"<br(?:\s*)/>", string.Empty, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            }
            stringToStrip = Regex.Replace(stringToStrip, "\"", "''", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            stringToStrip = Regex.Replace(stringToStrip, "&[^;]*;", string.Empty, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            stringToStrip = StripHtmlXmlTags(stringToStrip);
            return stringToStrip;
        }

        /// <summary>
        /// 格式化预览
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string StripForPreview(string content)
        {
            return StripHtmlXmlTags(content.Replace("<br>", "\n").Replace("<br/>", "\n").Replace("<br />", "\n").Replace("<p>", "\n").Replace("'", "&#39;"));
        }

        /// <summary>
        /// 去掉所有的html标签及xml标签
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string StripHtmlXmlTags(string content)
        {
            return Regex.Replace(content, "<[^>]+>", string.Empty, RegexOptions.Compiled | RegexOptions.IgnoreCase);
        }

        /// <summary>
        /// 去掉script标签
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string StripScriptTags(string content)
        {
            content = Regex.Replace(content, "<script((.|\n)*?)</script>", "", RegexOptions.Multiline | RegexOptions.IgnoreCase);
            return Regex.Replace(content, "\"javascript:", "", RegexOptions.Multiline | RegexOptions.IgnoreCase);
        }

    }
}
